package com.hongning.railway.ui.main.mine;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bruce.pickerview.popwindow.DatePickerPopWin;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.databinding.ActivityLoginBinding;
import com.hongning.railway.databinding.ActivityPartymemberinfoBinding;
import com.hongning.railway.entity.resbean.MessageEvent;
import com.hongning.railway.entity.resbean.MzBean;
import com.hongning.railway.ui.categorylistnews.NewDetailFragmentViewModel;
import com.hongning.railway.ui.login.LoginViewModel;
import com.hongning.railway.ui.main.fragments.Minefragment;
import com.hongning.railway.ui.widget.MultiSelectPopWindow;
import com.hongning.railway.ui.widget.alertview.AlertView;
import com.hongning.railway.ui.widget.alertview.AlertViewOnItemClickListener;
import com.hongning.railway.util.Apputils;
import com.hongning.railway.util.TimeUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseFragment;

public class PartyMemberInfoActivity extends BaseFragment<ActivityPartymemberinfoBinding,PartyMemberInfoViewModel> {
    public static PartyMemberInfoActivity newInstance() {
        return new PartyMemberInfoActivity();
    }

        @Override
    public PartyMemberInfoViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(PartyMemberInfoViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.activity_partymemberinfo;
    }

    @Override
    public int initVariableId() {
        return BR.partymemberinfoviewModel;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.btnrdsjClick.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {

                DatePickerPopWin pickerPopWin = new DatePickerPopWin.Builder(getActivity(), new DatePickerPopWin.OnDatePickedListener() {
                    @Override
                    public void onDatePickCompleted(int year, int month, int day, String dateDesc) {
                        viewModel.loginEntity.get().getUserExtendInfo().setJoinpartydate(dateDesc);
                        viewModel.loginEntity.notifyChange();
                    }
                }).textConfirm("确定") //text of confirm button
                        .textCancel("取消") //text of cancel button
                        .btnTextSize(16) // button text size
                        .viewTextSize(20) // pick view text size
                        .colorCancel(Color.parseColor("#999999")) //color of cancel button
                        .colorConfirm(Color.parseColor("#009900"))//color of confirm button
                        .minYear(1990) //min year in loop
                        .maxYear(2550) // max year in loop
                        .showDayMonthYear(false) // shows like dd mm yyyy (default is false)
                        .dateChose(TimeUtils.getFormatTimeYY(new Date().getTime())) // date chose when init popwindow
                        .build();
                pickerPopWin.showPopWin(getActivity());
            }
        });


        viewModel.uc.btnzzmmClick.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
//                420520：团员,420521：党员,420522：预备党员,420523：群众
                List<String> tems=new ArrayList<>();
                tems.add("党员");
                tems.add("预备党员");
                tems.add("团员");
                tems.add("群众");
                new MultiSelectPopWindow.Builder(AppManager.getAppManager().currentActivity())
                        .setConfirmListener(new MultiSelectPopWindow.OnConfirmClickListener() {
                            @Override
                            public void onClick(MessageEvent messageEvent) {
                                viewModel.loginEntity.get().getUserExtendInfo().setPoliticalstanding(messageEvent.getSelectData());
                                viewModel.loginEntity.notifyChange();
                            }
                        })
                        .setDataArray(tems)
                        .setCancel("取消")
                        .setTitle("请选择")
                        .setConfirm("确定")
                        .build()
                        .show(binding.PartyMemberInfoZzmm);
            }
        });


        viewModel.uc.btnBirsDayClick.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {

                DatePickerPopWin pickerPopWin = new DatePickerPopWin.Builder(getActivity(), new DatePickerPopWin.OnDatePickedListener() {
                    @Override
                    public void onDatePickCompleted(int year, int month, int day, String dateDesc) {
//                        Toast.makeText(getActivity(), dateDesc, Toast.LENGTH_SHORT).show();
                        viewModel.loginEntity.get().getUserExtendInfo().setBirthday(dateDesc);
                        viewModel.loginEntity.notifyChange();
                    }
                }).textConfirm("确定") //text of confirm button
                        .textCancel("取消") //text of cancel button
                        .btnTextSize(16) // button text size
                        .viewTextSize(20) // pick view text size
                        .colorCancel(Color.parseColor("#999999")) //color of cancel button
                        .colorConfirm(Color.parseColor("#009900"))//color of confirm button
                        .minYear(1990) //min year in loop
                        .maxYear(2550) // max year in loop
                        .showDayMonthYear(false) // shows like dd mm yyyy (default is false)
                        .dateChose(TimeUtils.getFormatTimeYY(new Date().getTime())) // date chose when init popwindow
                        .build();
                pickerPopWin.showPopWin(getActivity());
            }
        });
        viewModel.uc.btnSexClick.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                new MultiSelectPopWindow.Builder(AppManager.getAppManager().currentActivity())
                        .setConfirmListener(new MultiSelectPopWindow.OnConfirmClickListener() {
                            @Override
                            public void onClick(MessageEvent messageEvent) {
                                viewModel.loginEntity.get().getUserExtendInfo().setSex(messageEvent.getSelectData());
                                viewModel.loginEntity.notifyChange();
                            }
                        })
                        .setDataArray(getSexData())
                        .setCancel("取消")
                        .setTitle("请选择")
                        .setConfirm("确定")
                        .build()
                        .show(binding.PartyMemberInfoZzmm);
            }
        });

        viewModel.uc.btnClick.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                new MultiSelectPopWindow.Builder(AppManager.getAppManager().currentActivity())
                        .setConfirmListener(new MultiSelectPopWindow.OnConfirmClickListener() {
                            @Override
                            public void onClick(MessageEvent messageEvent) {
                                viewModel.loginEntity.get().getUserExtendInfo().setNation(messageEvent.getSelectData());
                                viewModel.loginEntity.notifyChange();
                            }
                        })
                        .setDataArray(getSelecData())
                        .setCancel("取消")
                        .setTitle("请选择")
                        .setConfirm("确定")
                        .build()
                        .show(binding.PartyMemberInfoZzmm);
            }
        });

    }
    /**
     * 得到性别
     * @return
     */
    public List<String> getSexData()
    {
        List<String> selectDatas=new ArrayList<>();
        selectDatas.add("男");
        selectDatas.add("女");
        return  selectDatas;
    }

    /**
     * 得到选择的内容
     * @return
     */
 public List<String> getSelecData()
 {
     List<MzBean> mzBeans= new Gson().fromJson(Apputils.getDataInfo(getActivity(),"mz_data.json"),new TypeToken<List<MzBean>>(){}.getType());
    List<String> selectDatas=new ArrayList<>();
    if (mzBeans!=null&&!mzBeans.isEmpty())
    {
        for (MzBean mzBean:mzBeans)
        {
            selectDatas.add(mzBean.getName());
        }
    }
    return selectDatas;

 }
}
